package com.eben.model;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

/**
 * A class that represent the score for the game.
 * You can add, substract, divide or multiply your current score.
 * 
 * @author Jacob
 *
 */
public class Score {
	
	private double score;
	private double points = 0;
	private double scoreLastUpdate=-1;//Any int != 0 because then it wont draw that you start with 0 points.
	private Paint paint = new Paint();
	private String drawString=""; //Only used to not create lots of strings in draw()
	private Time gameTimer;
	
	/**
	 * Start with score = 0
	 */
	public Score() {
		this(0);
	}
	
	/**
	 * Start with the score you choose.
	 * Must bee greater than 0.
	 * 
	 * @param score - the score you want to start with.
	 */
	public Score(double score) {
		if(score > 0){
			this.score = score;
		}
		paint.setColor(Color.WHITE);
		paint.setTextSize(20);
		paint.setAntiAlias(true);
		gameTimer = new Time();
	}
	
	
	/**
	 * Multiply your score
	 * 
	 * @param multiply - the double you want to multiply the score with. Must be greater than 0 or else nothing will happen
	 */
	public void multiplyScore(double multiply){
		if(multiply > 0){
			score *= multiply;
		}
	}
	
	/**
	 * Divide your score
	 * 
	 * @param divide - the double you want to divide the score with. Must be greater than 0 or else nothing will happen
	 */
	public void divideScore(double divide){
		if(divide > 0){
			score /= divide;
		}
	}
	

	/**
	 * Add your score
	 * 
	 * @param add - the double you want to add to the score. Must be greater than 0 or else nothing will happen
	 */
	public void addScore(double add){
		if(add > 0){
			score += add;
		}
	}
	
	/**
	 * Substract your score
	 * 
	 * @param sub - is a double that you want to remove from the score . Must be greater than 0 or else nothing will happen
	 */
	public void substractScore(double sub){
		if(sub >= 0){
			score -= sub;
			if(score < 0){
				score = 0;
			}
		}
	}
		
	/**
	 * Gives you the score as an int instead of an double.
	 * the score will be rounded to closest integer.
	 * 
	 * @return the score as an int
	 */
	public int getScoreInt(){
		return (int)(getScore()+0.5);
	}
	
	/**
	 * Give you the current score
	 * 
	 * @return - your score
	 */
	public double getScore(){
		return score;
	}
	
	public int getTotalScoreInt(){
		return (int)(getTotalScore()+0.5);
	}
	
	public double getTotalScore(){
		return points*Math.pow(2, gameTimer.getTime()/20);
	}
	
	public void addPoint(){
		points += 1;
	}
	
	public int getPoints(){
		return (int)points;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = prime * getScoreInt();
		return result;
	}
	
	/**
	 * Gives true if the object is:
	 * - A score,Double,Integer with the same value as this Score.getScoreInt.
	 */
	@Override
	public boolean equals(Object o) {
		if(o instanceof Score){
			Score obj = (Score)o;
			return (getScoreInt() == obj.getScoreInt());
		}else if(o instanceof Integer){
			Integer obj = (Integer)o;
			return (getScoreInt() == obj.intValue());
		}else if(o instanceof Double){
			Double obj = (Double)o;
			return (getScoreInt() == (int)(obj.doubleValue() + 0.5));
		}
		return false;
	}
	
	public void startScoreTimer(){
		gameTimer.startTime();
	}
	
	public void draw(Canvas canvas) {
		score = getTotalScoreInt();
		if(!(score == scoreLastUpdate)){
			scoreLastUpdate = score;
			drawString = ""+(int)score;
		}
		canvas.drawText(drawString, 10, 30, paint);
	}
}
